<template>
  <div class="">
    <controller 
      :orders="orders"
      :memery="memery"
      :power="power"
      :wait-times="waitTimes"
      :catch-value="catchValue"
      :order-status="orderStatus"
      :read-position="readPosition"
      :base-time="baseTime"
      @changeProgramTime="changeProgramTime"
      @changeData="changeData"
      @changeMemery="changeMemery"
    />
    <user-interface
      :power="power"
      :q-ui="storeQUI"
      :program-time="programTime"
      :store-memery="storeMemery"
      :memery-need-border="JSON.stringify(memeryNeedBorder)"
      @changeData="changeData"
      @refreshProgram="refreshProgram"
    />
    <div class="display-none">
      {{ temp }}
    </div>
  </div>
</template>
<script>
/**
 * 数据层
 */
import controller from "./controller";
import userInterface from './userInterface.vue';
import generateFunCode from "@/utils/generateFunCode";
export default {
  name: 'Data',
  components: { controller, userInterface },
  data() {
    return {
      
      waitTimes: 0,
      temp: "",
      baseTime: 5000,
      orders: {},
      needFind: "110",
      catchValue: {
        order: [],
        status: -1,
      },
      programTime: 0,
      orderStatus: "q0",
      readPosition: 0,
      power: false,
      memery: [0,0,1,1,0,1,0],
      memeryNeedBorder: []
    };
  },
  computed: {
    storeQUI() {
      let baseInfo = this.orders.baseInfo || {};
      let finArr = [[],[],[]];
      let nowChecked = Number(this.orderStatus.slice(1));
      for (let i = 0; i < baseInfo.n; i++) {
        let obj = {name: `q${i}`, checked: 0, value: 0,catch: this.catchValue.status};
        // ## 检测每一位的数
        if (i <= nowChecked && nowChecked >= 0) {
          obj.checked = i === nowChecked ? 2 : 1;
          obj.value = this.needFind[i];
        }
        finArr[0].push(obj);
        // ## 回退
        let obj0 = {name: `q-${i}`, checked: false, value: 0};
        if (i <= -nowChecked && nowChecked < 0) {
          obj0.checked = true, obj0.value = i;
        }
        if (i) finArr[2].unshift(obj0);
      }
      // if (nowChecked < 0) {
      //   console.log("nowChecked is", nowChecked);
      //   for (let i = 0; i < -nowChecked; i++) {
      //     let obj = {name: `q${i}`, checked: false, value: 0};
      //     if (i > -nowChecked) obj.checked = true, obj.value = nowChecked;
      //     finArr[2].push(obj);
      //   }
      // }
      let returnTrue = {name: `q${baseInfo.n}`, checked: false};
      if (this.orderStatus === `q${baseInfo.n}`) returnTrue.checked = true;
      let returnFalse = {name: `q${baseInfo.n + 1}`, checked: false};
      if (this.orderStatus === `q${baseInfo.n + 1}`) returnFalse.checked = true;
      finArr[1].push(...[returnTrue, returnFalse]);
      return finArr;
    },
    storeMemery() {
      // by @this.memery  @this.readPosition @this.memeryNeedBorder
      let arr = [];
      this.memery.forEach((ele, index) => {
        let obj = {
          value: ele,
          checked: false,
          border: 0,
          borderColor: "#eee",
        };
        if (this.readPosition === index) obj.checked = true;
        // let border = this.memeryNeedBorder;
        // if (border[index]) {
        //   obj.border = border[index].value;
        //   // return true
        //   if (border[index].status === 1) obj.borderStatus = "rgb(145, 189, 255)";
        //   // return false
        //   else if (border[index].status === 2) obj.borderStatus = "red";
        // }
        arr.push(obj);
      });
      return arr;
    }
  },
  watch: {
    orderStatus(newVal) {
      let len = this.needFind.length;
      // return true
      if (newVal === `q${len}`) {
        // stop turing, then check catch
        // start
        let num = this.readPosition - len;
        // over
        let num1 = this.readPosition - 1;
        // if stop, will send needBorder Info to this.memeryNeedBorder
        for (let i = num; i <= num1; i++) {
          if (i === num) this.memeryNeedBorder[num] = {status: "success", value: 2};
          else if (i === num1) this.memeryNeedBorder[num1] = {status: "success", value: 3};
          else this.memeryNeedBorder[i] = {status: "success", value: 1};
        }
        if (num === num1) this.memeryNeedBorder[this.readPosition] = {status: "success", value: 4};
      }
      // return false
      if (newVal === `q${len + 1}`) {
        // stop turing, then check catch
        this.memeryNeedBorder[this.readPosition - 1] = {status: "fail", value: 4};
      }
      this.temp = new Date();
    },
    needFind(newVal) {
      this.createOrders(newVal);
    }
  },
  mounted() {
    this.createOrders("110");
  },
  methods: {
    changeProgramTime(data) {
      this.programTime = data;
    },
    refreshProgram() {
      this.power = false;
      this.orderStatus = "q0";
      this.readPosition = 0;
      this.catchValue = {
        order: [],
        status: -1,
      };
    },
    createOrders(data) {
      this.orders = generateFunCode.find(data);
    },
    changeMemery({position, data}) {
      this.memery[position] = data;
    },
    changeData({key, value}) {
      this[key] = value;
    }
  }
};
</script>